package cn.innosmart.aq.customize;

import android.app.Dialog;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import cn.innosmart.aq.R;
import cn.innosmart.aq.customize.switchbutton.CheckSwitchButton;
import cn.innosmart.aq.util.DisplayUtil;

/**
 * Created by David on 2015/8/4.
 */
public class PrioritySetDialog extends Dialog {
    private Context mContext;
    private Button bt_negative;
    private TextView tv_title;
    private CheckSwitchButton switchButton;
    private RadioGroup radioGroup;
    private RadioButton radioNormal;
    private RadioButton radioImportant;
    private RadioButton radioHighImportant;
    private CharSequence mNegativeButtonText;
    private View.OnClickListener mNegativeButtonListener;
    private int type;
    private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rb_normal:
                    type = 1;
                    break;
                case R.id.rb_important:
                    type = 2;
                    break;
                case R.id.rb_high_important:
                    type = 3;
                    break;
            }
        }
    };
    private OnChangeListener onChangeListener;
    private CharSequence mPositiveButtonText;
    private View.OnClickListener mPositiveButtonListener;
    private View.OnClickListener mBtnOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bt_positive:
                    mPositiveButtonListener.onClick(v);
                    break;
                case R.id.bt_negative:
                    mNegativeButtonListener.onClick(v);
                    break;
            }
        }
    };
    private Button bt_positive;

    public PrioritySetDialog(Context context, int type) {
        super(context, R.style.dialog);
        init(context, type);
    }

    public PrioritySetDialog(Context context, int theme, int type) {
        super(context, theme);
        init(context, type);
    }

    public PrioritySetDialog(Context context, boolean cancelable, OnCancelListener cancelListener, int type) {
        super(context, cancelable, cancelListener);
        init(context, type);
    }

    private void init(Context context, int type) {
        this.mContext = context;
        this.type = type;
        View contentView = View.inflate(getContext(), R.layout.dialog_priority_set, null);
        setContentView(contentView);
        bt_positive = (Button) findViewById(R.id.bt_positive);
        bt_positive.setOnClickListener(mBtnOnClickListener);
        bt_negative = (Button) findViewById(R.id.bt_negative);
        bt_negative.setOnClickListener(mBtnOnClickListener);
        tv_title = (TextView) findViewById(R.id.tv_title);
        switchButton = (CheckSwitchButton) findViewById(R.id.switch_buton);
        radioNormal = (RadioButton) findViewById(R.id.rb_normal);
        setRadioNormal();
        radioImportant = (RadioButton) findViewById(R.id.rb_important);
        setRadioImportant();
        radioHighImportant = (RadioButton) findViewById(R.id.rb_high_important);
        setRadioHighImportant();
        radioGroup = (RadioGroup) findViewById(R.id.rg_group);
        radioGroup.setOnCheckedChangeListener(onCheckedChangeListener);

        //getWindow().setWindowAnimations(R.anim.alpha_in);
        switch (type) {
            case 1:
                radioNormal.setChecked(true);
                break;
            case 2:
                radioImportant.setChecked(true);
                break;
            case 3:
                radioHighImportant.setChecked(true);
                break;
        }
        /**
         * 强制设置点击外部不取消对话框
         */
        setCanceledOnTouchOutside(false);
    }

    private void setRadioNormal() {
        SpannableStringBuilder word = new SpannableStringBuilder();
        final String one = mContext.getString(R.string.priority_normal_simple);
        final String two = mContext.getString(R.string.priority_normal);
        int start = 0;
        int end = one.length();
        word.append(one);
        word.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(mContext,17)), start, end,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        word.append(two);
        start = end;
        end += two.length();
        word.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(mContext,13)), start, end,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        radioNormal.setText(word);
    }

    private void setRadioImportant() {
        SpannableStringBuilder word = new SpannableStringBuilder();
        final String one = mContext.getString(R.string.priority_important_simple);
        final String two = mContext.getString(R.string.priority_important);
        int start = 0;
        int end = one.length();
        word.append(one);
        word.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(mContext,17)), start, end,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        word.append(two);
        start = end;
        end += two.length();
        word.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(mContext,13)), start, end,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        radioImportant.setText(word);
    }
    private void setRadioHighImportant() {
        SpannableStringBuilder word = new SpannableStringBuilder();
        final String one = mContext.getString(R.string.priority_high_important_simple);
        final String two = mContext.getString(R.string.priority_high_important);
        int start = 0;
        int end = one.length();
        word.append(one);
        word.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(mContext,17)), start, end,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        word.append(two);
        start = end;
        end += two.length();
        word.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(mContext,13)), start, end,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        radioHighImportant.setText(word);
    }
    public void setTitle(CharSequence title) {
        tv_title.setText(title);
    }

    public void setTitle(int titleId) {
        tv_title.setText(mContext.getText(titleId));
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        super.setCanceledOnTouchOutside(cancel);
    }

    public void setPositiveButton(int textId, View.OnClickListener listener) {
        mPositiveButtonText = mContext.getText(textId);
        bt_positive.setVisibility(View.VISIBLE);
        bt_positive.setText(mPositiveButtonText);
        mPositiveButtonListener = listener;
    }

    public void setPositiveButton(CharSequence text, View.OnClickListener listener) {
        mPositiveButtonText = text;
        bt_positive.setVisibility(View.VISIBLE);
        bt_positive.setText(mPositiveButtonText);
        mPositiveButtonListener = listener;
    }

    public void setNegativeButton(int textId, View.OnClickListener listener) {
        mNegativeButtonText = mContext.getText(textId);
        bt_negative.setVisibility(View.VISIBLE);
        bt_negative.setText(mNegativeButtonText);
        mNegativeButtonListener = listener;
    }

    public void setNegativeButton(CharSequence text, View.OnClickListener listener) {
        mNegativeButtonText = text;
        bt_negative.setVisibility(View.VISIBLE);
        bt_negative.setText(mNegativeButtonText);
        mNegativeButtonListener = listener;
    }

    @Override
    public void show() {
        super.show();
    }

    public void setOnChangeListener(OnChangeListener OnChangeListener) {
        this.onChangeListener = OnChangeListener;
    }

    public void checkChange() {
        if (onChangeListener != null) {
            onChangeListener.onChange(type);
        }
    }

    public interface OnChangeListener {
        void onChange(int type);
    }
}
